import inquirer from "inquirer";
import { exec } from "child_process";

const command = process.argv[2]; // [start: 运行, publish: 发布]

// 选择环境
const { mode } = await inquirer.prompt({
  name: "mode",
  type: "list",
  message: command === "start" ? "请选择项目运行环境: " : "请选择项目部署环境: ",
  choices: command === "start" ? ["开发环境", "开发环境[桌面端]"] : ["生产环境[桌面端Windows]", "生产环境[桌面端Mac]"],
  default: "开发环境",
});

// 执行脚本
if (command === "start") {
  console.log("\x1b[32m", ` √ 运行命令已下达, 正在执行中 - Loading...`);
  let startRes = null;
  switch (mode) {
    case "开发环境":
      startRes = exec(`vite --mode dev`);
      break;
    case "开发环境[桌面端]":
      startRes = exec(`vite --mode dev | electron .`);
      break;
  }
  startRes.stdout.on("data", (data) => console.log(`  ${data.trim()}`));
  startRes.stderr.on("data", (data) => console.error(data));
}

if (command === "publish") {
  console.log("\x1b[32m", ` √ 发布命令已下达, 正在执行中 - Loading...`);
  let publishRes = null;
  switch (mode) {
    case "生产环境[桌面端Windows]":
      publishRes = exec(`vite build --mode prod && electron-builder`);
      break;
    case "生产环境[桌面端Mac]":
      publishRes = exec(`vite build --mode prod && electron-builder --mac --arm64`);
      break;
  }
  publishRes.stdout.on("data", (data) => console.log(`  ${data.trim()}`));
  publishRes.stderr.on("data", (data) => console.error(data));
}
